import request from '@/utils/request'
import * as mock from './mock/address'

const isMock = import.meta.env.VITE_API_MOCK === 'true'

/**
 * 获取用户地址列表
 * @returns {Promise}
 */
export function getAddressList() {
  if (isMock) {
    return Promise.resolve(mock.getAddressList())
  }
  return request({
    url: '/address/list',
    method: 'get'
  })
}

/**
 * 添加新地址
 * @param {Object} data 地址数据
 * @returns {Promise}
 */
export function addAddress(data) {
  if (isMock) {
    return Promise.resolve(mock.addAddress(data))
  }
  return request({
    url: '/address/manager',
    method: 'post',
    data
  })
}

/**
 * 更新地址
 * @param {Object} data 地址数据
 * @returns {Promise}
 */
export function updateAddress(data) {
  if (isMock) {
    return Promise.resolve(mock.updateAddress(data))
  }
  return request({
    url: '/address/manager',
    method: 'post',
    data
  })
}

/**
 * 删除地址
 * @param {String} id 地址ID
 * @returns {Promise}
 */
export function deleteAddress(id) {
  if (isMock) {
    return Promise.resolve(mock.deleteAddress(id))
  }
  return request({
    url: '/address/delete',
    method: 'post',
    data: { "id":id }
  })
}

/**
 * 设置默认地址
 * @param {String} id 地址ID
 * @returns {Promise}
 */
export function setDefaultAddress(id) {
  if (isMock) {
    return Promise.resolve(mock.setDefaultAddress(id))
  }
  return request({
    url: '/address/def',
    method: 'post',
    data: {"id" :id }
  })
}

/**
 * 获取默认地址
 * @param {String} id 地址ID
 * @returns {Promise}
 */
export function getDefaultAddress(id) {
  if (isMock) {
    return Promise.resolve(mock.setDefaultAddress(id))
  }
  return request({
    url: '/address/default',
    method: 'get',
    params: { id }
  })
}